package team.tunan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.tunan.common.HttpCodeEnum;
import team.tunan.dto.StockinDto;
import team.tunan.dto.StockinPageDto;
import team.tunan.entity.Stock;
import team.tunan.entity.Stockin;
import team.tunan.exception.BusinessException;
import team.tunan.mapper.StockMapper;
import team.tunan.mapper.StockinMapper;
import team.tunan.service.IEmployeeService;
import team.tunan.service.IStockService;
import team.tunan.service.IStockinService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.tunan.service.ISupplierService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 进货表 服务实现类
 * </p>
 *
 * @author sqx
 * @since 2023-12-24
 */
@Service
@Slf4j
public class StockinServiceImpl extends ServiceImpl<StockinMapper, Stockin> implements IStockinService {
    @Autowired
    private StockinMapper stockinMapper;

    @Autowired
    private IStockService iStockService;

    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private ISupplierService iSupplierService;



    @Override
    @Transactional
    public int insertStockin(Stockin stockin) {
        if(stockin.getQuantity() <= 0) {
            throw new BusinessException(HttpCodeEnum.OPERATION_ERROR.getCode(), "进货数量必须大于0");
        }
        stockinMapper.insert(stockin);
        iStockService.addStockNumber(stockin.getStockId(),stockin.getQuantity());
        return 1;
    }

    @Override
    public Page<StockinDto> selectPage(StockinPageDto stockinPageDto) {
        Page<Stockin> page = new Page<>(stockinPageDto.getCurrentPage(), stockinPageDto.getPageSize());
        LambdaQueryWrapper<Stockin> queryWrapper = new LambdaQueryWrapper<>();
        if(stockinPageDto.getStockId()!=null){
            queryWrapper.eq(Stockin::getStockId,stockinPageDto.getStockId());
        }
        if(stockinPageDto.getSupplierId()!=null){
            queryWrapper.eq(Stockin::getSupplierId,stockinPageDto.getSupplierId());
        }
        if(stockinPageDto.getRole()==2){
            queryWrapper.eq(Stockin::getEmployeeId,stockinPageDto.getEmployeeId());
        }
        //查询分页信息
        Page<Stockin> stockinPage = stockinMapper.selectPage(page, queryWrapper);
        Page<StockinDto> stockinDtoPage = new Page<>();
        //转化为需要的Dto
        List<Stockin> records = stockinPage.getRecords();
        List<StockinDto> collect = records.stream().map(item -> {
            StockinDto stockinDto = new StockinDto();
            BeanUtils.copyProperties(item, stockinDto);
            //对Dto中的员工名，商品名，进货商名进行赋值
            stockinDto.setEmployeeName(iEmployeeService.selectByEmployeeId(item.getEmployeeId()).getUserName());
            stockinDto.setSupplierName(iSupplierService.getById(item.getSupplierId()).getSupplierName());
            stockinDto.setStockName(iStockService.getById(item.getStockId()).getStockName());
            return stockinDto;
    }).collect(Collectors.toList());
        //组装分页信息
        stockinDtoPage.setRecords(collect);
        stockinDtoPage.setPages(stockinPage.getPages());
        stockinDtoPage.setSize(stockinPage.getSize());
        stockinDtoPage.setTotal(stockinPage.getTotal());
        stockinDtoPage.setCurrent(stockinPage.getCurrent());
        return stockinDtoPage;


    }
}
